/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package warehouse;

import system.ListType;
import ejb.warehouse.Articolo;
import ejb.warehouse.ArticoloFacadeRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import system.Utilities;

/**
 *
 * @author ciummo
 */
public class ArticleActionForm extends org.apache.struts.action.ActionForm {


    Utilities utility = new Utilities();
  
    
    private String codArticolo;
    private String nome;
    private String tipo;
    private Double prezzo;
    private String note;

    private List <Articolo> list;
    
    private final static String comunicatore= "Comunicatore";
    private final static String supporto ="Supporto";
    private final static String accessorio = "Accessorio";    

    private ArrayList listaTipi = new ArrayList();
    
    
    public String getCodArticolo() {
        return codArticolo;
    }

    public void setCodArticolo(String codArticolo) {
        this.codArticolo = codArticolo;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Double getPrezzo() {
        return prezzo;
    }

    public void setPrezzo(Double prezzo) {
        this.prezzo = prezzo;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }
    
    
    public List<Articolo> getList() {
        return list;
    }

    public void setList(List<Articolo> list) {
        this.list = list;
    }

    public Articolo getElement(int n) {
        return (Articolo)list.get(n);
    }    

    public ArrayList getListaTipi() {
        return listaTipi;
    }

    public void setListaTipi(ArrayList listaTipi) {
        this.listaTipi = listaTipi;
    }    
    
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
 

    /**
     *
     */
    public ArticleActionForm() {
        super();
        
        listaTipi.add(new ListType(1,comunicatore));
        listaTipi.add(new ListType(1,supporto));
        listaTipi.add(new ListType(1,accessorio));        
        
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        
        ArticoloFacadeRemote articoloFacade = null;
        
        try {
            articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");
        } catch (NamingException ex) {
            Logger.getLogger(ArticleActionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            if (articoloFacade.find(getCodArticolo()).equals(null)){

                errors.add("codArticolo", new ActionMessage("error.order.existArticle"));
            }
        }
        catch(NullPointerException npe){}
        
        if (getCodArticolo().isEmpty()){
            
            
            errors.add("codArticolo", new ActionMessage("error.article.code"));
        }
        
        if (getTipo().equals("0")){
            
            errors.add("tipo", new ActionMessage("error.article.type"));
        }
        
        if (getNome().isEmpty()){
            
            errors.add("nome", new ActionMessage("error.article.name"));
        }
        
        
        return errors;
    }
}
